package com.Servlet;
import com.Dao.GetUserDao;
import com.Dao.Imp.GetUserDaoImp;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//防止用户输入中文导致乱码
        resp.setContentType("text/html;charset=UTF-8");//防止浏览器端乱码
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        GetUserDao getUserDao=new GetUserDaoImp();
        int loginFlag=0;
        try {
            loginFlag=getUserDao.getUser(username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        switch (loginFlag){
            case 1:
                HttpSession session=req.getSession();
                session.setAttribute("username",username);
                resp.sendRedirect("/");
                break;
            case 2:
                resp.sendRedirect("/userLogin.jsp?error=fail");
                break;
            case 3:
                resp.sendRedirect("/userLogin.jsp?error=pleLogin");
                break;
            case 4:
                resp.sendRedirect("/userLogin.jsp?error=forbid");
        }
    }
}
